<template>
  <div class="login_content">
    <div class="login_box">
      <!-- 用户头像 -->
      <div class="login_image">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单注册,在main.js中导入element-ui需要的组件库 -->
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // form属性数据
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 用户验证规则
      loginRules: {
        //用户名验证
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        //密码验证
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 表单的预验证
    submitForm() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        //发起请求,axios发起,main.js中已经定义了$http
        const { data: res } = await this.$http.post('login', this.loginForm)
        // console.dir(res);
        //弹框提示
        if (res.meta.status == 200) {
          this.$message({
            message: '登录成功了',
            type: 'success'
          })
          //将登录成功之后的token保存到客户端sessionStorage中
          window.sessionStorage.setItem('token', res.data.token)
          //通过编程式导航到主页,路由为'/home'
          this.$router.push('/home')
        } else {
          this.$message.error('登录失败了')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_content {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  border-radius: 5px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  .login_image {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    border: 3px solid #fff;
    overflow: hidden;
    background-color: #eee;
    position: absolute;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    left: 50%;
    top: -20%;
    transform: translateX(-50%);
    img {
      width: 100%;
      height: 100%;
    }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    padding: 0 30px;
    width: 100%;
    box-sizing: border-box;
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
